#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // TabWidget 标签页
    ui->tabWidget->setTabText(0,"第1页");
    ui->tabWidget->setTabText(1,"第2页");

    QLabel* label1 = new QLabel(ui->tab);
    label1->setText("这是第1页");
    label1->move(20, 20);

    QLabel* label2 = new QLabel(ui->tab_2);
    label2->setText("这是第2页");
    label2->move(20, 20);
    // 设置垂直水平布局管理器
    QGroupBox* group1 = new QGroupBox("group1");
    QVBoxLayout* vlayout1 = new QVBoxLayout();
    QHBoxLayout* hlayout1 = new QHBoxLayout();
    hlayout1->addWidget(ui->pushButton_add);
    hlayout1->addWidget(ui->pushButton_sub);
    vlayout1->addLayout(hlayout1);
    vlayout1->addWidget(ui->tabWidget);
    this->setLayout(hlayout1);
    this->setLayout(vlayout1);
    group1->setLayout(vlayout1);


    // QVBoxLayout 垂直布局管理器
    QGroupBox* group2 = new QGroupBox("group2");
    QVBoxLayout* vlayout = new QVBoxLayout();
    QPushButton* button1 = new QPushButton("button1");
    QPushButton* button2 = new QPushButton("button2");
    vlayout->addWidget(button1);
    vlayout->addWidget(button2);
    group2->setLayout(vlayout);

    // QHBoxLayout 水平布局管理器
    QGroupBox* group3 = new QGroupBox("group3");
    QHBoxLayout* hlayout = new QHBoxLayout();
    QPushButton* button3 = new QPushButton("button3");
    QPushButton* button4 = new QPushButton("button4");
    // 添加空白
    QSpacerItem* spacer = new QSpacerItem(50, 50);

    hlayout->addWidget(button3);
    hlayout->addSpacerItem(spacer);// 放在两个Widget中间，就插入了空白间隔
    hlayout->addWidget(button4);
    group3->setLayout(hlayout);

    // QGirdLayout 网格布局管理器
    QGroupBox* group4 = new QGroupBox("group4");
    QGridLayout* glayout = new QGridLayout();
    QPushButton* button5 = new QPushButton("button5");
    QPushButton* button6 = new QPushButton("button6");
    glayout->addWidget(button5, 0, 0);
    glayout->addWidget(button6, 1, 1);
    button5->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button6->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    //单独这样设置，发现按钮并没有被行拉伸，因为Widget中的SizePolisy默认按钮垂直方向不拉伸，所以要进行上面的设置
    glayout->setRowStretch(0, 2);
    glayout->setRowStretch(1, 1);
    group4->setLayout(glayout);

    // 表单布局管理器(特殊的网格布局)
    QGroupBox* group5 = new QGroupBox("group5");
    QLabel* label_1 = new QLabel("姓名");
    QLabel* label_2 = new QLabel("年龄");
    QLabel* label_3 = new QLabel("电话");

    edit1 = new QLineEdit();
    edit2 = new QLineEdit();
    edit3 = new QLineEdit();
    QPushButton* button = new QPushButton("提交");

    QFormLayout* flayout = new QFormLayout();
    flayout->addRow(label_1, edit1);
    flayout->addRow(label_2, edit2);
    flayout->addRow(label_3, edit3);
    flayout->addRow(nullptr, button);
    group5->setLayout(flayout);

    connect(button, &QPushButton::clicked, this, &Widget::handle);

    // 设置总体布局
    QGridLayout* layout = new QGridLayout();
    layout->addWidget(group1, 0, 0, 1, 1);
    layout->addWidget(group2, 0, 1, 1, 1);
    layout->addWidget(group3, 1, 0, 1, 1);
    layout->addWidget(group4, 1, 1, 1, 1);
    layout->addWidget(group5, 2, 1, 1, 1);
    // 设置拉伸系数，之前每个列都是1:1的，可以设置各列都是多少比例
    // 如果拉伸系数设置为0，则不参与拉伸，长度固定不变
    layout->setColumnStretch(0, 1);
    layout->setColumnStretch(1, 2);
    layout->setRowStretch(0, 0);
    layout->setRowStretch(1, 3);
    this->setLayout(layout);


}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_add_clicked()
{
    QWidget* widget = new QWidget();
    int count = ui->tabWidget->count();
    ui->tabWidget->addTab(widget, QString("第") + QString::number(count + 1) + QString("页"));

    QLabel* label = new QLabel(widget);
    label->setText(QString("这是第") + QString::number(count + 1) + QString("页"));
    label->move(20, 20);
    ui->tabWidget->setCurrentIndex(count);
}

void Widget::on_pushButton_sub_clicked()
{
    int index = ui->tabWidget->currentIndex();
    ui->tabWidget->removeTab(index);
}

void Widget::on_tabWidget_currentChanged(int index)
{
    qDebug() << "当前为第 " << index + 1 << " 页";
}

void Widget::handle()
{
    QString name = edit1->text();
    QString gender = edit2->text();
    QString phone = edit3->text();

    qDebug() << "姓名：" << name;
    qDebug() << "性别：" << gender;
    qDebug() << "电话：" << phone;
}
